package chapter1.JavaObject.src.com.kkb.tz.bean;

import java.io.*;

/**
 *
 * 序列化保存对象
 * 反序列化恢复对象
 * @author : wushikai
 * <p>
 * date : 2021-02-18
 */

public class Teacher implements Serializable {
    private String name;

    public Teacher() {
    }

    public Teacher(String name, int age) {
        this.name = name;
        this.age = age;
    }

    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }


    public static void main(String[] args) throws IOException, ClassNotFoundException {

        Teacher teacher = new Teacher();
        teacher.setAge(33);
        teacher.setName("王艳");



        System.out.println("开始序列化");
        FileOutputStream fs = new FileOutputStream("t.ser");
        ObjectOutputStream os = new ObjectOutputStream(fs);
        os.writeObject(teacher);
        os.close();
        fs.close();

        System.out.println("开始反序列化");
        FileInputStream fileInputStream = new FileInputStream("t.ser");
        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
        Teacher o = (Teacher)objectInputStream.readObject();
        System.out.println(o);
        fileInputStream.close();
        objectInputStream.close();

    }





}